package 统计算法.找到数组的中间位置;

import java.util.Arrays;

public class zdszzjwz {
    public static void main(String[] args) {
//        int[] arr = {1,-1,4};
        int[] arr = {4,0};
        System.out.println(findMiddleIndex(arr));
    }


    public static int findMiddleIndex(int[] nums) {
        int total = Arrays.stream(nums).sum();
        int sum = 0;
        for (int i = 0; i < nums.length ; i++) {
            if (2*sum + nums[i] == total){
                return i;
            }
            sum += nums[i];
        }
        return -1;
    }
}
